<template>
  <div class="user-switcher">
    <h3>当前用户: {{ currentUser.name }} ({{ roleLabel }})</h3>
    <div class="user-list">
      <button
        v-for="user in users"
        :key="user.id"
        @click="switchUser(user.id)"
        :class="['user-btn', { active: user.id === currentUser.id }]"
        :disabled="loading"
      >
        <div class="user-info">
          <span class="user-name">{{ user.name }}</span>
          <span class="user-role">{{ roleLabels[user.role] }}</span>
          <span v-if="user.department" class="user-dept">{{ user.department }}</span>
        </div>
      </button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useApplicationStore } from '@/store/application'
import type { UserRole } from '@/services/models'

const store = useApplicationStore()

// 角色标签映射
const roleLabels: Record<UserRole, string> = {
  employee: '员工',
  head: '部门主管',
  finance: '财务',
  gm: '总经理'
}

// 当前用户信息
const currentUser = computed(() => store.currentUser)
const users = computed(() => store.users)
const loading = computed(() => store.loading)

// 当前用户角色标签
const roleLabel = computed(() => roleLabels[currentUser.value.role])

// 切换用户
const switchUser = async (userId: string) => {
  await store.switchUser(userId)
}
</script>

<style scoped>
.user-switcher {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  margin-bottom: 20px;
}

.user-switcher h3 {
  margin: 0 0 15px 0;
  color: #333;
  font-size: 16px;
}

.user-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 10px;
}

.user-btn {
  display: block;
  width: 100%;
  padding: 12px;
  border: 2px solid #e0e0e0;
  border-radius: 6px;
  background: #f8f9fa;
  cursor: pointer;
  transition: all 0.3s ease;
  text-align: left;
}

.user-btn:hover:not(:disabled) {
  border-color: #3498db;
  background: #ecf0f1;
}

.user-btn.active {
  border-color: #3498db;
  background: #3498db;
  color: white;
}

.user-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.user-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.user-name {
  font-weight: 600;
  font-size: 14px;
}

.user-role {
  font-size: 12px;
  color: #666;
}

.user-dept {
  font-size: 11px;
  color: #999;
}

.user-btn.active .user-role,
.user-btn.active .user-dept {
  color: rgba(255, 255, 255, 0.8);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .user-list {
    grid-template-columns: 1fr;
  }
  
  .user-switcher {
    padding: 15px;
  }
}
</style> 